#!/bin/bash
set -euo pipefail

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
MAKEFILEPATH=$SCRIPTPATH/../Makefile
VERSION=$(make -s -f $MAKEFILEPATH version)
BUILD_DIR=$SCRIPTPATH/../build/k8s-resources/$VERSION

PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
HELM_VERSION="3.17.1"
NAMESPACE="kube-system"
K8S_VERSION="1.32.2"
SUFFIX=""

USAGE=$(cat << EOM
  Usage: generate-k8s-yaml  [-n <K8s_NAMESPACE>]
  Generates the kubernetes yaml resource files from the helm chart
  and places them into the build dir.
  Example: generate-k8s-yaml -n kube-system
          Optional:
            -n NAMESPACE    Kubernetes namespace
            -k VERSION      Target Kubernetes version (default is ${K8S_VERSION})
            -s SUFFIX       String appended to generated file and directory names
            -v              Enable verbose output
EOM
)

# Process our input arguments
while getopts "n:k:s:v" opt; do
  case ${opt} in
    n ) # K8s namespace
        NAMESPACE=$OPTARG
      ;;
    k ) # K8s version
        K8S_VERSION=$OPTARG
      ;;
    s ) # Suffix
        SUFFIX=$OPTARG
      ;;
    v ) # Verbose
        set -x
      ;;
    \? )
        echo "$USAGE" 1>&2
        exit
      ;;
  esac
done

INDV_RESOURCES_DIR=${BUILD_DIR}/individual-resources${SUFFIX}
TAR_RESOURCES_FILE=${BUILD_DIR}/individual-resources${SUFFIX}.tar
AGG_RESOURCES_YAML=${BUILD_DIR}/all-resources${SUFFIX}.yaml
mkdir -p $INDV_RESOURCES_DIR

QP_INDV_RESOURCES_DIR=${BUILD_DIR}/individual-resources-queue-processor${SUFFIX}
QP_TAR_RESOURCES_FILE=${BUILD_DIR}/individual-resources-queue-processor${SUFFIX}.tar
QP_AGG_RESOURCES_YAML=${BUILD_DIR}/all-resources-queue-processor${SUFFIX}.yaml
mkdir -p $QP_INDV_RESOURCES_DIR

HELM=$BUILD_DIR/helm
if [[ ! -e $HELM ]]; then
  curl -L https://get.helm.sh/helm-v$HELM_VERSION-$PLATFORM-amd64.tar.gz | tar zxf - -C $BUILD_DIR
  mv $BUILD_DIR/$PLATFORM-amd64/helm $BUILD_DIR/.
  rm -rf $BUILD_DIR/$PLATFORM-amd64
  chmod +x $HELM
fi

## IMDS Mode
$HELM template aws-node-termination-handler \
    --namespace $NAMESPACE \
    --kube-version ${K8S_VERSION} \
    --set targetNodeOs="linux windows" \
    $SCRIPTPATH/../config/helm/aws-node-termination-handler/ > $AGG_RESOURCES_YAML

## Queue Processor Mode
$HELM template aws-node-termination-handler \
    --namespace $NAMESPACE \
    --kube-version ${K8S_VERSION} \
    --set enableSqsTerminationDraining="true" \
    --set enableProbesServer="true" \
    $SCRIPTPATH/../config/helm/aws-node-termination-handler/ > $QP_AGG_RESOURCES_YAML

# IMDS mode - remove helm annotations from template
cat $AGG_RESOURCES_YAML | grep -v 'helm.sh\|app.kubernetes.io/managed-by: Helm' > $BUILD_DIR/helm_annotations_removed.yaml
mv $BUILD_DIR/helm_annotations_removed.yaml $AGG_RESOURCES_YAML

# Queue Processor Mode - remove helm annotations from template
cat $QP_AGG_RESOURCES_YAML | grep -v 'helm.sh\|app.kubernetes.io/managed-by: Helm' > $BUILD_DIR/helm_annotations_removed.yaml
mv $BUILD_DIR/helm_annotations_removed.yaml $QP_AGG_RESOURCES_YAML

# IMDS Mode
$HELM template aws-node-termination-handler \
    --namespace $NAMESPACE \
    --kube-version ${K8S_VERSION} \
    --set targetNodeOs="linux windows" \
    --output-dir $INDV_RESOURCES_DIR/ \
    $SCRIPTPATH/../config/helm/aws-node-termination-handler/

# Queue Processor Mode
$HELM template aws-node-termination-handler \
    --namespace $NAMESPACE \
    --kube-version ${K8S_VERSION} \
    --set enableSqsTerminationDraining="true" \
    --set enableProbesServer="true" \
    --output-dir $QP_INDV_RESOURCES_DIR/ \
    $SCRIPTPATH/../config/helm/aws-node-termination-handler/

# Queue Processor Mode - remove helm annotations from template
for i in $INDV_RESOURCES_DIR/aws-node-termination-handler/templates/*; do
  cat $i | grep -v 'helm.sh\|app.kubernetes.io/managed-by: Helm' > $BUILD_DIR/helm_annotations_removed.yaml
  mv $BUILD_DIR/helm_annotations_removed.yaml $i
done

# IMDS Mode - remove helm annotations from template
for i in $QP_INDV_RESOURCES_DIR/aws-node-termination-handler/templates/*; do
  cat $i | grep -v 'helm.sh\|app.kubernetes.io/managed-by: Helm' > $BUILD_DIR/helm_annotations_removed.yaml
  mv $BUILD_DIR/helm_annotations_removed.yaml $i
done

cd $INDV_RESOURCES_DIR/aws-node-termination-handler/ && tar cvf $TAR_RESOURCES_FILE templates/*
cd $QP_INDV_RESOURCES_DIR/aws-node-termination-handler/ && tar cvf $QP_TAR_RESOURCES_FILE templates/*
cd $SCRIPTPATH

echo "Generated aws-node-termination-handler kubernetes yaml resources files in:"
echo "    - $AGG_RESOURCES_YAML"
echo "    - $TAR_RESOURCES_FILE"
echo "    - $QP_AGG_RESOURCES_YAML"
echo "    - $QP_TAR_RESOURCES_FILE"
